package com.company.cloud.user.modules.mgr.service.impl;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant.YNEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.PasswordUtils;
import com.company.cloud.common.utils.StrSplitUtils;
import com.company.cloud.user.modules.api.request.UserMgrUpdateParam;
import com.company.cloud.user.modules.base.entity.UserInfo;
import com.company.cloud.user.modules.base.entity.UserRoleInfo;
import com.company.cloud.user.modules.base.service.UserInfoService;
import com.company.cloud.user.modules.base.service.UserRoleInfoService;
import com.company.cloud.user.modules.mgr.controller.req.UserRegisterByAdminParam;
import com.company.cloud.user.modules.mgr.service.UserMgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Service
public class UserMgrServiceImpl implements UserMgrService{
	
	@Autowired
	private UserInfoService  userInfoService;
	@Autowired
	private UserRoleInfoService userRoleInfoService;


	@Deprecated
	@Override
	public void register(UserRegisterByAdminParam param, LoginSimpleUserInfo userInfo) {
		ValidatorUtils.validate(param);

	}

	@Override
	public void update(UserMgrUpdateParam param, LoginSimpleUserInfo userInfo) {
		System.out.println(JSON.toJSONString(userInfo));
		ValidatorUtils.validate(param);
		UserInfo entity = userInfoService.getById(param.getId());
		if (null == entity ) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		if(param.getUserName().isEmpty()){
			throw new BusinessException("-6","账户名数据为空");
		}
		 Integer count = userInfoService.getBaseMapper().selectCount(new QueryWrapper<UserInfo>().eq("user_name", param.getUserName()).eq("user_state", 1).ne("id",param.getId()));
		if(count!=0){
			throw new BusinessException("-7","账户名数据已存在");
		}
		entity.setUserTypeOne(param.getUserTypeOne());
		entity.setUserName(param.getUserName());
		if(Objects.equals(param.getUserTypeOne(),"2")){
			entity.setUserName("");
		}
		entity.setUserTypeTwo(param.getUserTypeTwo());
		entity.setNickName(param.getNickName());
		entity.setEmail(param.getEmail());
		entity.setTel(param.getTel());
		entity.setQq(param.getQq());
		entity.setHeadImg(param.getHeadImg());
		entity.setDeptId(param.getDeptId());
		entity.setMobile(param.getMobile());
		entity.setIsDeptManager(param.getIsDeptManager());
		entity.setDataAuthorityType(param.getDataAuthorityType());
		entity.setDataAuthorityScope(StrSplitUtils.list2string(param.getDataScopes()));
		entity.setJobCode(StrSplitUtils.list2string(param.getJobs()));
		if (!StringUtils.isEmpty(param.getPassword())) {
			String[] passwords = PasswordUtils.getMd5Password(param.getPassword());
			entity.setPassword(passwords[0]);
			entity.setSalt(passwords[1]);
		}
		if(StrUtil.isNotEmpty(param.getCustType())){
			entity.setCustType(param.getCustType());
		}

		if(StrUtil.isNotEmpty(param.getCustCode())){
			entity.setCustCode(param.getCustCode());
		}
		if(StrUtil.isNotEmpty(param.getCustName())){
			entity.setCustName(param.getCustName());
		}
		entity.addEditParam(userInfo);
		userInfoService.updateById(entity);
		
		List<UserRoleInfo> userRoleList=new ArrayList<UserRoleInfo>();
		if(CollectionUtils.isEmpty(param.getRoles())) {
			userRoleInfoService.remove(new QueryWrapper<UserRoleInfo>().eq("user_id",  entity.getId()));
		}
		if(!CollectionUtils.isEmpty(param.getRoles())) {
			param.getRoles().forEach(item->{
				UserRoleInfo urInfo=new UserRoleInfo();
				urInfo.setCompanyNo(entity.getCompanyNo());
				urInfo.setUserId(entity.getId());
				urInfo.setRoleCode(item);
				urInfo.addCreateParam(userInfo);
				urInfo.addEditParam(userInfo);
				userRoleList.add(urInfo);
			});
		}
		userRoleInfoService.remove(new QueryWrapper<UserRoleInfo>().eq("user_id",  entity.getId()));
		if(userRoleList.size()>0) {
			userRoleInfoService.saveBatch(userRoleList);
		}
	}

	@Override
	public void updateUserState(Integer id, String state, LoginSimpleUserInfo userInfo) {
		UserInfo entity = userInfoService.getById(id);
		if (null == entity ) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		if (YNEnum.YES.getCode().equals(entity.getIsManager()) ) {
//			throw new BusinessException(UserErrorEnum.USER_IS_MANAGER);
			throw new BusinessException("");
		}
		if(entity.getUserState().equals(state)) {
			return;
		}
		entity.setUserState(state);
		entity.addEditParam(userInfo);
		userInfoService.updateById(entity);
	}

	
	@Override
	public void updateUserPassword(Integer id, String password, LoginSimpleUserInfo userInfo) {
		UserInfo entity = userInfoService.getById(id);
		if (null == entity ) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		String[] passwords= PasswordUtils.getMd5Password(password);
		entity.setPassword(passwords[0]);
		entity.setSalt(passwords[1]);
		entity.addEditParam(userInfo);
		userInfoService.updateById(entity);
	}
	
	

	


}
